//
//  HMStatusOriView.swift
//  sina
//
//  Created by Kent on 16/9/28.
//  Copyright © 2016年 Ming. All rights reserved.
//

import UIKit

private let margin = CGFloat(8)

class HMStatusOriView: UIView {

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    var bottomCons: Constraint?
    
    var viewModel: HMStatusViewModel? {
        didSet {
            
            userIconImgView.sd_setImage(with: URL(string: (viewModel?.status?.user?.avatar_large)!))
            
            nameLabel.text = viewModel?.status?.user?.name
            
            // membership lv imgview
        
            sentLabel.text = viewModel?.status?.created_at
            
            sourceLabel.text = viewModel?.status?.source
            
            contentText.text = viewModel?.status?.text
            
            let pCount = viewModel?.status?.pic_urls?.count ?? 0
            
            pictureCV.snp.updateConstraints { (make) in
                if pCount == 0 {
                    make.top.equalTo(contentText.snp.bottom).offset(margin)
                }
                make.size.equalTo(calculateCVSize(count: pCount))
            }
            pictureCV.pictureInfos = viewModel?.status?.pic_urls

        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        UISetup()
    }
    
    func UISetup() {
        // 分割线 UIView
        self.addSubview(separatorView)
        
        separatorView.snp.makeConstraints { (make) in
            make.left.right.top.equalTo(self)
            make.height.equalTo(12)
        }
        
        // 用户头像 ImageView
        self.addSubview(userIconImgView)
        
        userIconImgView.snp.makeConstraints { (make) in
            make.left.equalTo(self).offset(margin)
            make.top.equalTo(separatorView.snp.bottom).offset(margin)
            make.size.equalTo(CGSize(width: 40, height: 40))
        }
        
        // 用户名字 Label
        
        self.addSubview(nameLabel)
        
        nameLabel.snp.makeConstraints { (make) in
            make.left.equalTo(userIconImgView.snp.right).offset(margin)
            make.top.equalTo(userIconImgView)
        }
        
        
        // 会员等级 图片 imageView
        
        self.addSubview(membershipLv)
        
        membershipLv.snp.makeConstraints { (make) in
            make.left.equalTo(nameLabel.snp.right).offset(margin)
            make.top.equalTo(userIconImgView)
            make.size.equalTo(CGSize(width: 10, height: 10))
        }
        
        
        // 发送时间 Label
        
        self.addSubview(sentLabel)
        
        sentLabel.snp.makeConstraints { (make) in
            make.left.equalTo(nameLabel)
            make.bottom.equalTo(userIconImgView)
        }
        
        // 发送源 Label
        
        self.addSubview(sourceLabel)
        
        sourceLabel.snp.makeConstraints { (make) in
            make.left.equalTo(sentLabel.snp.right).offset(margin)
            make.bottom.equalTo(userIconImgView)
        }
        
        
        // 原创内容 文字 Label
        
        self.addSubview(contentText)
        
        contentText.snp.makeConstraints { (make) in
            make.left.equalTo(userIconImgView)
            make.right.equalTo(self).offset(-margin)
            make.top.equalTo(userIconImgView.snp.bottom).offset(margin)
            make.bottom.equalTo(contentText.snp.bottom)
        }
        
        // 原创图
        
        self.addSubview(pictureCV)
        
        pictureCV.snp.makeConstraints { (make) in
            make.top.equalTo(contentText.snp.bottom).offset(margin)
            make.left.equalTo(userIconImgView.snp.left)
            make.right.equalTo(self).offset(-margin)
            make.size.equalTo(CGSize.zero)
            make.bottom.equalTo(self)
        }
    
    }

    lazy var separatorView: UIView = {
        
        let v = UIView()
        v.backgroundColor = #colorLiteral(red: 0.9041042924, green: 0.8974847198, blue: 0.8892857432, alpha: 1)
        return v
        
    }()
    
    lazy var userIconImgView: UIImageView = {
        
        let imgView = UIImageView()
        imgView.image = #imageLiteral(resourceName: "avatar_default")
        imgView.cornerRadius = 20
        
        return imgView
    }()
    
    lazy var nameLabel = UILabel(title: "", fontSize: 13, textColor: UIColor.darkGray)
    
    lazy var membershipLv: UIImageView = UIImageView(image: #imageLiteral(resourceName: "common_icon_membership"))
    
    lazy var sentLabel = UILabel(title: "", fontSize: 13, textColor: UIColor.orange)
    
    lazy var sourceLabel: UILabel = {
        let l = UILabel()
        l.text = ""
        l.font = UIFont.systemFont(ofSize: 13)
        l.textColor = UIColor.darkGray
        l.numberOfLines = 1
        return l
    }()
    
    lazy var contentText: UILabel = {
        
        let l = UILabel()
        l.text = ""
        l.font = UIFont.systemFont(ofSize: 13)
        l.textColor = UIColor.darkGray
        l.numberOfLines = 0
        l.textAlignment = .left
        return l
        
    }()
    
    lazy var pictureCV: HMPictureCView = {
        
        let lo = UICollectionViewFlowLayout()
        
        lo.itemSize = CGSize(width: itemWidth, height: itemWidth)
        
        lo.minimumLineSpacing = pictureCellMargin
        lo.minimumInteritemSpacing = pictureCellMargin
        
        let cv = HMPictureCView(frame: CGRect.zero, collectionViewLayout: lo)
        
        cv.backgroundColor = UIColor.white
        
        return cv
        
    }()
    
    private func calculateCVSize(count: Int) -> CGSize {
        
        if count == 0 {
            return CGSize.zero
        }
        
        if count == 4 {
            let width = itemWidth * 2 + pictureCellMargin
            return CGSize(width: width, height: width)
        }
        
        let rowCount = CGFloat((count - 1) / 3 + 1)
        let height = rowCount * itemWidth + (rowCount - 1) * pictureCellMargin
        
        return CGSize(width: maxWidth, height: height)
    }


}





























